var util = require('../../utils/util.js');
var ajax = require('../../utils/ajax.js');
var common = require('../../utils/common.js');
var PageBase = require('../../common/PageBase.js');
var app = getApp();


Page(common.extendPage(PageBase, {
  data: {
    userStars: [
      '/imgs/valuation/order_pingjia_t.png',
      '/imgs/valuation/order_pingjia_f.png',
      '/imgs/valuation/order_pingjia_f.png',
      '/imgs/valuation/order_pingjia_f.png',
      '/imgs/valuation/order_pingjia_f.png'
    ],
    rank: 1
  },
  /**
   * 页面初始化
   */
  onLoad(options) {
    this._options = options;

  },

  // 星星点击事件
  starTap: function(e) {
    var index = e.currentTarget.dataset.index; // 获取当前点击的是第几颗星星
    var tempUserStars = this.data.userStars; // 暂存星星数组
    var len = tempUserStars.length; // 获取星星数组的长度

    for (var i = 0; i < len; i++) {
      if (i <= index) { // 小于等于index的是满心
        tempUserStars[i] = '/imgs/valuation/order_pingjia_t.png'
      } else { // 其他是空心
        tempUserStars[i] = '/imgs/valuation/order_pingjia_f.png'
      }
    }
    // 重新赋值就可以显示了
    this.setData({
      userStars: tempUserStars,
      rank: index + 1
    })

  },
  /**
   * 评价提交
   */
  _submit(evt) {
    var {
      valuation
    } = evt.detail.value;
    var paramsUrl = '/public/api/shops/addComments';
    var paramsData = {
      uid: wx.getStorageSync('uid'),
      mid: this._options.mid,
      start: this.data.rank,
      content: valuation
    }
    ajax.P({
      url: paramsUrl,
      data: paramsData,
      success: (res) => {
        wx.navigateBack();
      }
    })
  }
}));